package gallery;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by luweichao on 2015/1/28.
 */
public class SectorProgressView extends TextView {

    private RectF oval, ovalTwo;
    private Paint paintCircle, paintSector, paintCircleSmall;
    private int sweepAngle;
    private int width, height;

    public SectorProgressView(Context context) {
        this(context, null);
        init();
    }

    public SectorProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init();
    }

    private void init() {
        paintCircle = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintCircle.setColor(Color.GRAY);
        paintCircle.setStyle(Paint.Style.FILL);
        paintCircleSmall = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintCircleSmall.setColor(Color.BLACK);
        paintCircleSmall.setStyle(Paint.Style.FILL);
        paintSector = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintSector.setColor(Color.argb(220, 90, 200, 250));
        paintSector.setStyle(Paint.Style.FILL);
    }

    public SectorProgressView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public void setSweep(int sweep) {
        sweepAngle = sweep;
        postInvalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        oval = new RectF(0, 0, width, height);
        ovalTwo = new RectF(width / 7, height / 7, width * 6 / 7, height * 6 / 7);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawSector(canvas);
        super.onDraw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    private void drawSector(Canvas canvas) {
        canvas.drawArc(oval, 0, 360, true, paintCircle);
        canvas.drawArc(oval, 0, sweepAngle, true, paintSector);
        canvas.drawArc(ovalTwo, 0, 360, true, paintCircleSmall);
    }

}